package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/15 10:39
 */
public class CartServiceImpl implements CartService {
    @Override
    public List<Cart> listCart(String userId, Integer businessId) {
        List<Cart> list = null;
        try {
            CartDao cartDao = new CartDaoImpl();
            DBUtil.getConnection();
            list = cartDao.listCart(userId, businessId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) {
        int isRight = 0;
        try {
            DBUtil.getConnection();
            //开启事务
            DBUtil.beginTransaction();
            CartDao cartDao = new CartDaoImpl();
            UserDao userDao = new UserDaoImpl();
            //调用dao中的方法查询userId是否存在
            //如果存在就调用saveCart
            int count = userDao.getUserById(userId);
            if (count != 0) {
                isRight = cartDao.saveCart(userId, businessId, foodId);
            }
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                //回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            //关闭资源
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) {
        int isRight = 0;
        try {
            DBUtil.getConnection();
            //开启事务
            DBUtil.beginTransaction();
            CartDao cartDao = new CartDaoImpl();
            isRight = cartDao.updateCart(userId, businessId, foodId,quantity);
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                //回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            //关闭资源
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) {
        int isRight = 0;
        try {
            DBUtil.getConnection();
            //开启事务
            DBUtil.beginTransaction();
            CartDao cartDao = new CartDaoImpl();
            isRight = cartDao.removeCart(userId, businessId, foodId);
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                //回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            //关闭资源
            DBUtil.close();
        }
        return isRight;
    }
}
